package leetcode.backtrace;

import java.util.ArrayList;
import java.util.List;

public class Lc784 {

    List<String> ans;
    public List<String> letterCasePermutation(String s) {
        ans = new ArrayList<>();
        ans.add(s);
        getPermutation(s, 0);
        return ans;
    }

    private void getPermutation(String s, int start) {
        if (start == s.length())
            return;
        for (int i = start; i < s.length(); i++) {
            if (Character.isAlphabetic(s.charAt(i))) {
                StringBuffer temp = new StringBuffer(s.substring(0, i));
                temp.append(Character.isLowerCase(s.charAt(i)) ? Character.toUpperCase(s.charAt(i)) : Character.toLowerCase(s.charAt(i)));
                if (i != s.length() - 1) temp.append(s.substring(i + 1, s.length()));
                ans.add(temp.toString());
                getPermutation(temp.toString(), i + 1);
            }
        }
    }
}
